# 1022 简单：从根到叶的二进制数之和

# Definition for a binary tree node.
# class HOT.TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:

    def sumRootToLeaf(self, root: Optional[HOT.TreeNode]) -> int:
        def dfs(cur, lev):
            if cur is None:
                return 0
            lev = (lev << 1) | cur.val
            if cur.left is None and cur.right is None:
                return lev
            return dfs(cur.left, lev) + dfs(cur.right, lev)
        return dfs(root, 0)
